<template>
  <page-layout :show-back="true">
	  <view class="history-page">
		<!-- 日期分组容器 -->
		<scroll-view scroll-y class="history-list" @scrolltolower="loadMore" refresher-enabled 
			:refresher-triggered="isRefreshing" @refresherrefresh="onRefresh">
			<template v-if="groupedHistory.length > 0">
				<view v-for="(group, groupIndex) in groupedHistory" :key="groupIndex" class="history-group">
					<view class="date-header">
						<text class="date-text">{{group.date}}</text>
						<view class="date-line"></view>
					</view>
					
					<view v-for="(item, itemIndex) in group.items" :key="itemIndex" class="history-item" @tap="goToPostDetail(item.id)">
						<view class="post-content">
							<view class="post-text">
								<text class="post-title" v-if="item.title">{{item.title}}</text>
								<text class="post-desc">{{item.content}}</text>
							</view>
							<image v-if="item.cover" :src="item.cover" mode="aspectFill" class="post-cover"></image>
						</view>
						<view class="post-footer">
							<view class="author-info">
								<image :src="item.author.avatar" mode="aspectFill" class="author-avatar"></image>
								<text class="author-name">{{item.author.nickname}}</text>
							</view>
							<text class="view-time">{{item.viewTime}}</text>
						</view>
					</view>
				</view>
			</template>
			
			<!-- 空状态 -->
			<view v-if="groupedHistory.length === 0 && !isLoading" class="empty-state">
				<uni-icons type="calendar" size="64" color="#cccccc"></uni-icons>
				<text class="empty-text">暂无浏览记录</text>
				<button class="discover-btn" @tap="goToDiscover">去发现精彩内容</button>
			</view>
			
			<!-- 加载状态 -->
			<view v-if="isLoading" class="loading-more">
				<text>加载中...</text>
			</view>
			<view v-if="!isLoading && groupedHistory.length > 0 && !hasMore" class="no-more">
				<text>没有更多了</text>
			</view>
		</scroll-view>
	</view>
  </page-layout>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import uniIcons from '@dcloudio/uni-ui/lib/uni-icons/uni-icons.vue';
import PageLayout from "../../components/PageLayout.vue";

// 页面数据
const historyList = ref([]);
const page = ref(1);
const pageSize = ref(10);
const hasMore = ref(true);
const isLoading = ref(true);
const isRefreshing = ref(false);

// 将历史记录按日期分组
const groupedHistory = computed(() => {
	const groups = [];
	const dateMap = new Map();
	
	historyList.value.forEach(item => {
		const date = item.date;
		if (!dateMap.has(date)) {
			dateMap.set(date, []);
			groups.push({
				date: date,
				items: dateMap.get(date)
			});
		}
		dateMap.get(date).push(item);
	});
	
	return groups;
});

// 加载浏览历史列表
const loadHistoryList = async (isRefresh = false) => {
	if (isRefresh) {
		page.value = 1;
		hasMore.value = true;
		historyList.value = [];
	}
	
	isLoading.value = true;
	
	// 模拟请求延迟
	setTimeout(() => {
		// 模拟接口返回数据
		const today = '今天';
		const yesterday = '昨天';
		const earlierDates = ['本周', '上周', '更早'];
		
		const mockHistory = Array.from({ length: 10 }, (_, i) => {
			const id = i + 1 + (page.value - 1) * 10;
			const hasTitle = Math.random() > 0.3;
			const hasImage = Math.random() > 0.4;
			
			// 确定日期分组
			let date;
			if (page.value === 1) {
				// 第一页有今天和昨天的浏览记录
				date = i < 4 ? today : (i < 7 ? yesterday : earlierDates[0]);
			} else if (page.value === 2) {
				// 第二页有上周和更早的浏览记录
				date = i < 5 ? earlierDates[1] : earlierDates[2];
			} else {
				// 第三页都是更早的浏览记录
				date = earlierDates[2];
			}
			
			return {
				id: id,
				title: hasTitle ? `这是一个很棒的帖子标题 #${id}` : '',
				content: `这是帖子的内容，分享了一些有趣的事情和想法。这是第${id}条内容，希望大家喜欢！${hasTitle ? '' : '这条内容没有标题，但是内容很精彩。'}`,
				cover: hasImage ? `https://picsum.photos/400/300?${id}` : '',
				date: date,
				viewTime: date === today ? `${Math.floor(Math.random() * 12) + 1}小时前` : 
						(date === yesterday ? `昨天 ${Math.floor(Math.random() * 12) + 10}:${Math.floor(Math.random() * 60)}` : 
						`${Math.floor(Math.random() * 10) + 1}天前`),
				author: {
					id: Math.floor(Math.random() * 100) + 1,
					nickname: `创作者${Math.floor(Math.random() * 100) + 1}`,
					avatar: `https://picsum.photos/100?${Math.floor(Math.random() * 100) + 1}`
				}
			};
		});
		
		// 模拟没有更多数据
		if (page.value >= 3) {
			hasMore.value = false;
		}
		
		if (isRefresh) {
			historyList.value = mockHistory;
			isRefreshing.value = false;
		} else {
			historyList.value = [...historyList.value, ...mockHistory];
		}
		
		isLoading.value = false;
	}, 1000);
};

// 加载更多
const loadMore = () => {
	if (!hasMore.value || isLoading.value) return;
	page.value++;
	loadHistoryList();
};

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true;
	loadHistoryList(true);
};

// 跳转到帖子详情
const goToPostDetail = (postId) => {
	uni.navigateTo({
		url: `/pages/post/detail?id=${postId}`
	});
};

// 清空历史记录
const clearHistory = () => {
	uni.showModal({
		title: '清空历史',
		content: '确定要清空所有浏览记录吗？此操作不可恢复',
		confirmColor: '#FF4D4F',
		success: (res) => {
			if (res.confirm) {
				// 清空历史记录
				historyList.value = [];
				uni.showToast({
					title: '已清空历史记录',
					icon: 'success'
				});
			}
		}
	});
};

// 返回上一页
const goBack = () => {
	console.log('返回按钮被点击');
	uni.showToast({
		title: '正在返回...',
		icon: 'none',
		duration: 300
	});
	
	setTimeout(() => {
		uni.navigateBack({
			delta: 1,
			fail: () => {
				uni.switchTab({
					url: '/pages/user/me'
				});
			}
		});
	}, 200);
};

// 去发现页面
const goToDiscover = () => {
	uni.switchTab({
		url: '/pages/index/index'
	});
};

// 页面加载时获取数据
onMounted(() => {
	loadHistoryList();
});
</script>

<style>
.history-page {
	background-color: #f8f8f8;
	min-height: 100vh;
}

.history-list {
	height: calc(100vh - 88rpx);
	padding: 0;
	box-sizing: border-box;
}

.history-group {
	margin-bottom: 20px;
	width: 100%;
	padding: 0 30rpx;
	box-sizing: border-box;
}

.date-header {
	display: flex;
	align-items: center;
	margin: 30rpx 0;
	width: 100%;
	box-sizing: border-box;
}

.date-text {
	font-size: 30rpx;
	font-weight: 500;
	color: #666;
	margin-right: 20rpx;
	white-space: nowrap;
}

.date-line {
	flex: 1;
	height: 1px;
	background-color: #eee;
}

.history-item {
	background-color: #ffffff;
	padding: 30rpx;
	border-radius: 8px;
	margin-bottom: 24rpx;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
	width: 100%;
	box-sizing: border-box;
}

.post-content {
	display: flex;
	margin-bottom: 15px;
	width: 100%;
	box-sizing: border-box;
}

.post-text {
	flex: 1;
	margin-right: 10px;
	overflow: hidden;
}

.post-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
}

.post-desc {
	font-size: 28rpx;
	color: #666;
	line-height: 1.5;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}

.post-cover {
	width: 200rpx;
	height: 140rpx;
	border-radius: 8rpx;
	object-fit: cover;
}

.post-footer {
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-top: 1px solid #f0f0f0;
	padding-top: 20rpx;
}

.author-info {
	display: flex;
	align-items: center;
}

.author-avatar {
	width: 48rpx;
	height: 48rpx;
	border-radius: 50%;
	margin-right: 16rpx;
}

.author-name {
	font-size: 26rpx;
	color: #666;
}

.view-time {
	font-size: 24rpx;
	color: #999;
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding-top: 200rpx;
	padding-bottom: 100rpx;
	width: 100%;
	box-sizing: border-box;
	padding-left: 30rpx;
	padding-right: 30rpx;
}

.empty-text {
	font-size: 32rpx;
	color: #999;
	margin: 40rpx 0;
}

.discover-btn {
	background-color: #0078ff;
	color: white;
	font-size: 32rpx;
	padding: 20rpx 40rpx;
	border-radius: 100rpx;
	border: none;
	width: 400rpx;
}

.loading-more, .no-more {
	text-align: center;
	padding: 40rpx 0;
	color: #999;
	font-size: 28rpx;
	width: 100%;
	box-sizing: border-box;
}
</style> 